//
//  SelfInformationViewController.m
//  RENTVISITOR
//
//  Created by wonder on 2017/8/25.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "SelfInformationViewController.h"
#import "RentVieitor.h"
#import "EditNameViewController.h"
#import "PhoneViewController.h"
#import "SexViewController.h"
#import "EditReNameViewController.h"
#import "EditBirViewController.h"
#import "EditEmailViewController.h"
#import "EdHeadViewController.h"


@interface SelfInformationViewController ()

@end

@implementation SelfInformationViewController {
    
    NSDictionary *info;
}


- (void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"updata" object:nil];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"个人信息";
    self.view.backgroundColor = BackgroundColor;
    //接受信息修改通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uodatanotification) name:@"update" object:nil];
    [self creatLeftView];
    [self creatView];
    [self loadData];
}

- (void)creatLeftView {
    
    // 自定义导航栏左侧按钮
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 10,15 );
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"箭头左"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}

//请求数据
- (void)loadData {
    
    NSDictionary *dataDic = User_listsValue;
    NSString *url = [NSString stringWithFormat:@"%@member/info/show",Main_Url];
    NSString *number = dataDic[@"number"];
    NSString *token = dataDic[@"token"];
    NSString *md5String = [NSString stringWithFormat:@"%@%@%@%@",number,token,APPKEY_URL,url];
    //32位小写
    md5String = [LCMD5Tool MD5ForLower32Bate:md5String];
    NSDictionary *param = @{
                            @"number":number,
                            @"token":token,
                            @"sign":md5String
                            };
    [AFHttpTool requestWihtUrl:url params:param success:^(id response) {
        
        
        if ([response[@"code"] isEqualToString:@"10000"]) {
            
            NSDictionary *data = response[@"data"];
            data = [Utils nullDic:data];
            [self reloadData:data];
        }
        
        
    } failure:^(NSError *err) {
        
        
    }];
}

//刷新数据
- (void)reloadData:(NSDictionary *)dataDic {
    
    info = [dataDic copy];
    UIView *nicknameView = (UIView *)[self.view viewWithTag:101];
    UILabel *nicknameLabel = (UILabel *)[nicknameView viewWithTag:201];
    nicknameLabel.text = dataDic[@"nickname"];
    
    UIView *mobileView = (UIView *)[self.view viewWithTag:102];
    UILabel *mobileLabel = (UILabel *)[mobileView viewWithTag:202];
    mobileLabel.text = dataDic[@"mobile"];
    
    UIView *emailView = (UIView *)[self.view viewWithTag:103];
    UILabel *emailLabel = (UILabel *)[emailView viewWithTag:203];
    emailLabel.text = dataDic[@"email"];
    
    UIView *sexView = (UIView *)[self.view viewWithTag:104];
    UILabel *sexLabel = (UILabel *)[sexView viewWithTag:204];
    NSString *sexStr = dataDic[@"sex"];
    if ([sexStr isEqualToString:@"1"]) {
        sexLabel.text = @"男";
    }else if ([sexStr isEqualToString:@"2"]) {
        sexLabel.text = @"女";
    }else if ([sexStr isEqualToString:@"3"]) {
        sexLabel.text = @"未选择";
    }
    
    
    NSString *yy = dataDic[@"birthday_yyyy"];
    NSString *mm = dataDic[@"birthday_mm"];
    NSString *dd = dataDic[@"birthday_dd"];
    NSString *birthday = [NSString stringWithFormat:@"%@-%@-%@",yy,mm,dd];
    UIView *birthdayView = (UIView *)[self.view viewWithTag:105];
    UILabel *birthdayLabel = (UILabel *)[birthdayView viewWithTag:205];
    birthdayLabel.text = birthday;
    
    UIView *nameView = (UIView *)[self.view viewWithTag:106];
    UILabel *nameLabel = (UILabel *)[nameView viewWithTag:206];
    nameLabel.text = dataDic[@"name"];


}

//创建视图
- (void)creatView {
    
    NSArray *textArray = @[@"头像",@"昵称",@"手机号",@"电子邮箱",@"性别",@"生日",@"真实姓名"];
    NSArray *titleArray =  @[@"头像",@"租租侠",@"13796210735",@"992960076@qq.com",@"男",@"1994-04-17",@"吴彦祖"];
    //选项视图
    CGFloat heigt = 50;
    for (int i=0; i<textArray.count; i++) {
        
        UIView *selectView = [[UIView alloc] init];
        selectView.backgroundColor = [UIColor whiteColor];
        selectView.tag = 100+i;
        if (i == textArray.count-1) {
            
            selectView.frame = CGRectMake(0,20+heigt*i , KWidth, heigt);

        }else {
            
            selectView.frame = CGRectMake(0,10+heigt*i , KWidth, heigt);
        }
        
        [self.view addSubview:selectView];
        
        //添加手势
        UITapGestureRecognizer * PrivateLetterTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAvatarView:)];
        PrivateLetterTap.numberOfTouchesRequired = 1; //手指数
        PrivateLetterTap.numberOfTapsRequired = 1; //tap次数
        [selectView addGestureRecognizer:PrivateLetterTap];
        
        UILabel *selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, KWidth/2, 30)];
        selectLabel.text = textArray[i];
        selectLabel.font = [UIFont systemFontOfSize:15];
        selectLabel.textAlignment = NSTextAlignmentLeft;
        selectLabel.textColor = [Utils colorConvertFromString:@"#101010"];
        [selectView addSubview:selectLabel];
        
        
        UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
        selectButton.frame = CGRectMake(KWidth-20, 20, 5, 10);
        [selectButton setBackgroundImage:[UIImage imageNamed:@"箭头-右"] forState:UIControlStateNormal];
        [selectView addSubview:selectButton];
    
        if (i == 0) {
                
                UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(KWidth-70, 10, 30, 30)];
                imgView.image = [UIImage imageNamed:@"用户_我"];
                [selectView addSubview:imgView];
        }else {
                
                UILabel *phoneLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(KWidth/2, 10, KWidth/2-40, 30)];
                phoneLabel1.text = titleArray[i];
                phoneLabel1.tag = 200+i;
                phoneLabel1.font = [UIFont systemFontOfSize:13];
                phoneLabel1.textAlignment = NSTextAlignmentRight;
                phoneLabel1.textColor = [Utils colorConvertFromString:@"#666666"];
                [selectView addSubview:phoneLabel1];
        }
        

        
        
        //分割线
        CALayer *MlineLayer5 = [CALayer layer];
        MlineLayer5.backgroundColor = [[Utils colorConvertFromString:@"#cfcfcf"] CGColor];
        MlineLayer5.frame = CGRectMake(20 ,heigt-1, KWidth,10);
        MlineLayer5.backgroundColor = [[Utils colorConvertFromString:@"#F0F0F0"] CGColor];
        [selectView.layer addSublayer:MlineLayer5];
    }

}

- (void)tapAvatarView:(UIGestureRecognizer *)recognizer {
    
    if (recognizer.view.tag == 100) {
        
        //修改昵称
        EdHeadViewController *edVc = [[EdHeadViewController alloc] init];
//        edVc.name = info[@"nickname"];
        [self.navigationController pushViewController:edVc animated:YES];
        
    }else if (recognizer.view.tag == 101){
        
        //修改昵称
        EditNameViewController *edVc = [[EditNameViewController alloc] init];
        edVc.name = info[@"nickname"];
        [self.navigationController pushViewController:edVc animated:YES];
        
    }else if (recognizer.view.tag == 102){
        
        //绑定手机号
        PhoneViewController *phoneVc = [[PhoneViewController alloc] init];
        phoneVc.phone = info[@"mobile"];
        [self.navigationController pushViewController:phoneVc animated:YES];
        
    }else if (recognizer.view.tag == 103){
        
        //邮箱
        //邮箱认证
        EditEmailViewController *email = [[EditEmailViewController alloc] init];
        email.email = info[@"email"];
        email.phone = info[@"mobile"];
        [self.navigationController pushViewController:email animated:YES];
        
    }else if (recognizer.view.tag == 104){
        
        //性别
        SexViewController *sexVc = [[SexViewController alloc] init];
        sexVc.getsex = info[@"sex"];
        [self.navigationController pushViewController:sexVc animated:YES];
        
    }else if (recognizer.view.tag == 105){
        
        //生日
        NSString *yy = info[@"birthday_yyyy"];
        NSString *mm = info[@"birthday_mm"];
        NSString *dd = info[@"birthday_dd"];
        NSString *birthday = [NSString stringWithFormat:@"%@-%@-%@",yy,mm,dd];
        EditBirViewController *birthVc = [[EditBirViewController alloc] init];
        birthVc.date = birthday;
        [self.navigationController pushViewController:birthVc animated:YES];
        
    }else if (recognizer.view.tag == 106){
        
        //真实姓名
        EditReNameViewController *relVc = [[EditReNameViewController alloc] init];
        relVc.name = info[@"name"];
        [self.navigationController pushViewController:relVc animated:YES];
    }
    
}

- (void)uodatanotification {
    
    [self loadData];
}

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
